package com.example.demo.controller;

// import com.example.demo.command.FlowableCommandExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 命令模式演示控制器
 */
@RestController
@RequestMapping("/api/command-pattern")
public class CommandPatternController {

    // @Autowired
    // private FlowableCommandExample commandExample;

    /**
     * 基本命令模式演示
     */
    @GetMapping("/basic-commands")
    public Map<String, Object> demonstrateBasicCommands() {
        try {
            // commandExample.demonstrateBasicCommands();
            
            Map<String, Object> response = new HashMap<>();
            response.put("message", "基本命令模式演示完成");
            response.put("status", "success");
            
            return response;
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("message", "基本命令模式演示失败：" + e.getMessage());
            response.put("status", "error");
            return response;
        }
    }

    /**
     * 自定义命令模式演示
     */
    @GetMapping("/custom-commands")
    public Map<String, Object> demonstrateCustomCommands() {
        try {
            // commandExample.demonstrateCustomCommands();
            
            Map<String, Object> response = new HashMap<>();
            response.put("message", "自定义命令模式演示完成");
            response.put("status", "success");
            
            return response;
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("message", "自定义命令模式演示失败：" + e.getMessage());
            response.put("status", "error");
            return response;
        }
    }

    /**
     * 命令链模式演示
     */
    @GetMapping("/command-chain")
    public Map<String, Object> demonstrateCommandChain() {
        try {
            // commandExample.demonstrateCommandChain();
            
            Map<String, Object> response = new HashMap<>();
            response.put("message", "命令链模式演示完成");
            response.put("status", "success");
            
            return response;
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("message", "命令链模式演示失败：" + e.getMessage());
            response.put("status", "error");
            return response;
        }
    }

    /**
     * 可撤销命令模式演示
     */
    @GetMapping("/undoable-commands")
    public Map<String, Object> demonstrateUndoableCommands() {
        try {
            // commandExample.demonstrateUndoableCommands();
            
            Map<String, Object> response = new HashMap<>();
            response.put("message", "可撤销命令模式演示完成");
            response.put("status", "success");
            
            return response;
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("message", "可撤销命令模式演示失败：" + e.getMessage());
            response.put("status", "error");
            return response;
        }
    }

    /**
     * 异步命令模式演示
     */
    @GetMapping("/async-commands")
    public Map<String, Object> demonstrateAsyncCommands() {
        try {
            // commandExample.demonstrateAsyncCommands();
            
            Map<String, Object> response = new HashMap<>();
            response.put("message", "异步命令模式演示完成");
            response.put("status", "success");
            
            return response;
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("message", "异步命令模式演示失败：" + e.getMessage());
            response.put("status", "error");
            return response;
        }
    }

    /**
     * 命令历史记录演示
     */
    @GetMapping("/command-history")
    public Map<String, Object> demonstrateCommandHistory() {
        try {
            // commandExample.demonstrateCommandHistory();
            
            Map<String, Object> response = new HashMap<>();
            response.put("message", "命令历史记录演示完成");
            response.put("status", "success");
            
            return response;
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("message", "命令历史记录演示失败：" + e.getMessage());
            response.put("status", "error");
            return response;
        }
    }

    /**
     * 命令模式说明
     */
    @GetMapping("/explanation")
    public Map<String, Object> getCommandPatternExplanation() {
        Map<String, Object> explanation = new HashMap<>();
        
        Map<String, Object> basicCommands = new HashMap<>();
        basicCommands.put("description", "使用Flowable内置命令类");
        basicCommands.put("examples", new String[]{"StartProcessInstanceCmd", "CompleteTaskCmd", "GetTasksByProcessInstanceIdCmd"});
        basicCommands.put("useCase", "标准流程操作");
        
        Map<String, Object> customCommands = new HashMap<>();
        customCommands.put("description", "创建自定义命令类");
        customCommands.put("examples", new String[]{"CustomStartProcessCommand", "CustomApprovalCommand", "CustomNotificationCommand"});
        customCommands.put("useCase", "业务特定操作");
        
        Map<String, Object> commandChain = new HashMap<>();
        commandChain.put("description", "将多个命令组合成命令链");
        commandChain.put("examples", new String[]{"CommandChain", "顺序执行", "错误处理"});
        commandChain.put("useCase", "复杂业务流程");
        
        Map<String, Object> undoableCommands = new HashMap<>();
        undoableCommands.put("description", "支持撤销和重做的命令");
        undoableCommands.put("examples", new String[]{"UndoableStartProcessCommand", "UndoCommand", "RedoCommand"});
        undoableCommands.put("useCase", "需要撤销功能的操作");
        
        Map<String, Object> asyncCommands = new HashMap<>();
        asyncCommands.put("description", "异步执行命令");
        asyncCommands.put("examples", new String[]{"AsyncCommand", "CommandStatusQuery", "异步处理"});
        asyncCommands.put("useCase", "长时间运行的操作");
        
        explanation.put("basicCommands", basicCommands);
        explanation.put("customCommands", customCommands);
        explanation.put("commandChain", commandChain);
        explanation.put("undoableCommands", undoableCommands);
        explanation.put("asyncCommands", asyncCommands);
        
        return explanation;
    }
}
